﻿'/* Copyright (c) 2009, Graeme Grant (graeme@live.com.au)
' * All rights reserved.
' *
' * Redistribution and use in source and binary forms, with or without
' * modification, are permitted provided that the following conditions are met:
' *
' *   * Redistributions of source code must retain the above copyright
' *     notice, this list of conditions and the following disclaimer.
' * 
' *   * Redistributions in binary form must reproduce the above copyright
' *     notice, this list of conditions and the following disclaimer in the
' *     documentation and/or other materials provided with the distribution.
' * 
' *   * The name Graeme Grant may not be used to endorse or promote products
' *     derived from this software without specific prior written permission.
' *
' * THIS SOFTWARE IS PROVIDED BY GRAEME GRANT ``AS IS'' AND ANY
' * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
' * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
' * DISCLAIMED. IN NO EVENT SHALL Dr. WPF BE LIABLE FOR ANY
' * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
' * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
' * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
' * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
' * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
' * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
' */

Imports System.Xml.Serialization
Imports System.IO
Imports System.Drawing

<Serializable()> _
Public Class Listing

#Region "Constructor"

    Public Sub New()

    End Sub

    Public Sub New(ByVal Details As Hashtable)
        ExtractDetails(Details)
    End Sub

    Friend Sub New(ByVal _json As String)
        ExtractDetails(CType(JSON.JsonDecode(_json), Hashtable)("results")(0))
    End Sub

#End Region

#Region "Public Properties"

    <XmlElement("listing_id")> _
    Private _listing_id As Long
    Public Property ListingID() As Long
        Get
            Return _listing_id
        End Get
        Set(ByVal value As Long)
            _listing_id = value
        End Set
    End Property

    <XmlElement("state")> _
    Private _state As Enums.listing_state
    Public Property State() As Enums.listing_state
        Get
            Return _state
        End Get
        Set(ByVal value As Enums.listing_state)
            _state = value
        End Set
    End Property

    <XmlElement("title")> _
    Private _title As String
    Public Property Title() As String
        Get
            Return _title
        End Get
        Set(ByVal value As String)
            _title = value
        End Set
    End Property

    <XmlElement("url")> _
    Private _url As String
    Public Property URL() As String
        Get
            Return _url
        End Get
        Set(ByVal value As String)
            _url = value
        End Set
    End Property

    <XmlElement("image_url_25x25")> _
    Private _image_url_25x25 As String
    Public Property ImageURL25x25() As String
        Get
            Return _image_url_25x25
        End Get
        Set(ByVal value As String)
            _image_url_25x25 = value
        End Set
    End Property

    <XmlElement("image_url_50x50")> _
    Private _image_url_50x50 As String
    Public Property ImageURL50x50() As String
        Get
            Return _image_url_50x50
        End Get
        Set(ByVal value As String)
            _image_url_50x50 = value
        End Set
    End Property

    <XmlElement("image_url_75x75")> _
    Private _image_url_75x75 As String
    Public Property ImageURL75x75() As String
        Get
            Return _image_url_75x75
        End Get
        Set(ByVal value As String)
            _image_url_75x75 = value
        End Set
    End Property

    <XmlElement("image_url_155x125")> _
    Private _image_url_155x125 As String
    Public Property ImageURL155x125() As String
        Get
            Return _image_url_155x125
        End Get
        Set(ByVal value As String)
            _image_url_155x125 = value
        End Set
    End Property

    <XmlElement("_image_url_200x200")> _
    Private _image_url_200x200 As String
    Public Property ImageURL200x200() As String
        Get
            Return _image_url_200x200
        End Get
        Set(ByVal value As String)
            _image_url_200x200 = value
        End Set
    End Property

    <XmlElement("image_url_430xN")> _
    Private _image_url_430xN As String
    Public Property ImageURL430xN() As String
        Get
            Return _image_url_430xN
        End Get
        Set(ByVal value As String)
            _image_url_430xN = value
        End Set
    End Property

    <XmlElement("creation_epoch")> _
    Private _creation As Date
    Public Property Creation() As Date
        Get
            Return _creation
        End Get
        Set(ByVal value As Date)
            _creation = value
        End Set
    End Property

    <XmlElement("views")> _
    Private _views As Integer
    Public Property Views() As Integer
        Get
            Return _views
        End Get
        Set(ByVal value As Integer)
            _views = value
        End Set
    End Property

    'Private _tags As String
    'Public Property Tags() As String
    '    Get
    '        Return _tags
    '    End Get
    '    Set(ByVal value As String)
    '        _tags = value
    '    End Set
    'End Property

    <XmlElement("tags")> _
    Private _tags As ArrayList
    Public Property Tags() As ArrayList
        Get
            Return _tags
        End Get
        Set(ByVal value As ArrayList)
            _tags = value
        End Set
    End Property

    'Private _materials As String
    'Public Property Materials() As String
    '    Get
    '        Return _materials
    '    End Get
    '    Set(ByVal value As String)
    '        _materials = value
    '    End Set
    'End Property

    <XmlElement("materials")> _
    Private _materials As ArrayList
    Public Property Materials() As ArrayList
        Get
            Return _materials
        End Get
        Set(ByVal value As ArrayList)
            _materials = value
        End Set
    End Property

    <XmlElement("price")> _
    Private _price As Decimal
    Public Property Price() As Decimal
        Get
            Return _price
        End Get
        Set(ByVal value As Decimal)
            _price = value
        End Set
    End Property

    <XmlElement("currency_code")> _
    Private _currency_code As String
    Public Property CurrencyCode() As String
        Get
            Return _currency_code
        End Get
        Set(ByVal value As String)
            _currency_code = value
        End Set
    End Property

    <XmlElement("user_id_epoch")> _
    Private _ending As Date
    Public Property Ending() As Date
        Get
            Return _ending
        End Get
        Set(ByVal value As Date)
            _ending = value
        End Set
    End Property

    <XmlElement("user_id")> _
    Private _user_id As Integer
    Public Property UserID() As Integer
        Get
            Return _user_id
        End Get
        Set(ByVal value As Integer)
            _user_id = value
        End Set
    End Property

    <XmlElement("user_name")> _
    Private _user_name As String
    Public Property UserName() As String
        Get
            Return _user_name
        End Get
        Set(ByVal value As String)
            _user_name = value
        End Set
    End Property

    <XmlElement("quantity")> _
    Private _quantity As Integer
    Public Property Quantity() As Integer
        Get
            Return _quantity
        End Get
        Set(ByVal value As Integer)
            _quantity = value
        End Set
    End Property

    <XmlElement("description")> _
    Private _description As String
    Public Property Description() As String
        Get
            Return _description
        End Get
        Set(ByVal value As String)
            _description = value
        End Set
    End Property

    <XmlElement("lat")> _
    Private _lat As Single
    Public Property Lat() As Single
        Get
            Return _lat
        End Get
        Set(ByVal value As Single)
            _lat = value
        End Set
    End Property

    <XmlElement("lon")> _
    Private _lon As Single
    Public Property Lon() As Single
        Get
            Return _lon
        End Get
        Set(ByVal value As Single)
            _lon = value
        End Set
    End Property

    <XmlElement("city")> _
    Private _city As String
    Public Property City() As String
        Get
            Return _city
        End Get
        Set(ByVal value As String)
            _city = value
        End Set
    End Property

    <XmlElement("section_id")> _
    Private _section_id As Integer
    Public Property SectionId() As Integer
        Get
            Return _section_id
        End Get
        Set(ByVal value As Integer)
            _section_id = value
        End Set
    End Property

    <XmlElement("section_title")> _
    Private _section_title As String
    Public Property SectionTitle() As String
        Get
            Return _section_title
        End Get
        Set(ByVal value As String)
            _section_title = value
        End Set
    End Property

    <XmlElement("favorite_creation_epoch")> _
    Private _favorite_creation As Date
    Public Property FavoriteCreation() As Date
        Get
            Return _favorite_creation
        End Get
        Set(ByVal value As Date)
            _favorite_creation = value
        End Set
    End Property

    <XmlElement("hsv_color")> _
    Private _hsv_color As ColorHSV
    Public Property HSVcolor() As ColorHSV
        Get
            Return _hsv_color
        End Get
        Set(ByVal value As ColorHSV)
            _hsv_color = value
        End Set
    End Property

    <XmlElement("rgb_color")> _
    Private _rgb_color As Color
    Public Property RGBcolor() As Color
        Get
            Return _rgb_color
        End Get
        Set(ByVal value As Color)
            _rgb_color = value
        End Set
    End Property

#End Region

#Region "Support Methods & Functions"

    Private Sub ExtractDetails(ByVal Details As Hashtable)

        _listing_id = JSON.ToInteger(Details("listing_id"))
        _favorite_creation = JSON.DecodeDateTime(Details("favorite_creation_epoch"))

        Select Case Details("state")
            Case "active" : _state = Enums.listing_state.Active
            Case "alchemy" : _state = Enums.listing_state.Alchemy
            Case "expired" : _state = Enums.listing_state.Expired
            Case "removed" : _state = Enums.listing_state.Removed
            Case "sold_out" : _state = Enums.listing_state.Soldout
            Case "edit" : _state = Enums.listing_state.Edit
            Case "frozen" : _state = Enums.listing_state.Frozen
            Case "vacation" : _state = Enums.listing_state.Frozen
        End Select

        _title = Details("title")
        _url = Details("url")
        _image_url_25x25 = Details("image_url_25x25")
        _image_url_50x50 = Details("image_url_50x50")
        _image_url_75x75 = Details("image_url_75x75")
        _image_url_155x125 = Details("image_url_155x125")
        _image_url_200x200 = Details("image_url_200x200")
        _image_url_430xN = Details("image_url_430xN")
        _creation = JSON.DecodeDateTime(Details("creation_epoch"))
        _views = JSON.ToInteger(Details("views"))
        _tags = Details("tags")
        _materials = Details("materials")
        _price = CDec(Details("price"))
        _currency_code = Details("currency_code")
        _ending = JSON.DecodeDateTime(Details("ending_epoch"))
        _user_id = JSON.ToInteger(Details("user_id"))
        _user_name = Details("user_name")
        _quantity = JSON.ToInteger(Details("quantity"))
        _hsv_color = New ColorHSV(Details("hsv_color"))
        _rgb_color = New ColorRGB(CType(Details("rgb_color"), String)).Color
        _description = Details("description")
        _lat = CSng(Details("lat"))
        _lon = CSng(Details("lon"))
        _city = Details("city")
        _section_id = JSON.ToInteger(Details("section_id"))
        _section_title = Details("section_title")

    End Sub

#End Region

End Class